package sctu.edu.java.year2016.team06.luoxin.day20160607.chapter15_2;

/**尝试开发一个窗体，在窗体中有两个按钮，一个是“开始”按钮，一个是“结束”按钮，当用户
 * 单击“开始”按钮时，在控制台中继续输入打印一句话，当用户单击“结束”按钮时，
 * Created by Administrator on 2016/6/7.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class PrintFrame extends JFrame {

    private JPanel contentPane;

    Timer timer;
    int index = 1;
    private JTextArea jta;

    //JDK 1.8
    public static void main(String[] args) {
        PrintFrame frame = new PrintFrame();
        frame.setVisible(true);
    }

    public PrintFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(350, 200);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.SOUTH);

        JButton jbStart = new JButton("开始打印");
        panel.add(jbStart);
        timer = new Timer(500, new ActionListener() {                 //每500毫秒打印1次
            public void actionPerformed(ActionEvent e) {
                jta.append("打印" + "第" + index + "行\r\n");
                index++;
            }
        });
        jbStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                timer.start();//开始
            }
        });
        JButton jbEnd = new JButton("停止打印");
        panel.add(jbEnd);
        jbEnd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                timer.stop();//停止
            }
        });
        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);
        jta = new JTextArea();
        scrollPane.setViewportView(jta);
    }

}
